package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.ydd.common.R;
import com.itheima.ydd.dto.PageInfo;
import com.itheima.ydd.dto.YddArticleDto;
import com.itheima.ydd.pojo.YddArticle;
import com.itheima.ydd.service.YddArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("ydd_article")
public class YddArticleController {
    @Autowired
    private YddArticleService yddArticleService;

    /**
     * 文章管理分页查询
     * @param start_time 创建时间
     * @param end_time   参数结束时间
     * @param title     标题
     * @param cateid     分类id
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<R<PageInfo<YddArticleDto>>> findPageOrTitleOrCateOrTime(String start_time,
                                                                                  String end_time,
                                                                                  String title,
                                                                                  String cateid,
                                                                                  String page,
                                                                                  String limit){
        return yddArticleService.findPageOrTitleOrCateOrTime(start_time,end_time,title,cateid,page,limit);
    }

    /**
     * 根据id删除文章
     * @param id
     * @return
     */
    @GetMapping("del")
    public ResponseEntity<R> removeById(@RequestParam String id){
        yddArticleService.removeById(id);
        return ResponseEntity.ok(R.success(null));
    }

    @PostMapping("add")
    public ResponseEntity<R> save(@RequestBody YddArticle yddArticle){
        //yddArticle.setCreateDate(1649667172000L);
        yddArticleService.save(yddArticle);
        return ResponseEntity.ok(R.success(null));
    }

    /**
     * 根据id查找单个数据,用于修改回显
     * @param id
     * @return
     */
    @GetMapping("rowInfo")
    public ResponseEntity<R<YddArticle>> findOne(@RequestParam String id){
        YddArticle yddArticle = yddArticleService.getById(id);
        return ResponseEntity.ok(R.success(yddArticle));
    }

    /**
     * 修改文章
     * @param yddArticleDto
     * @return
     */
    @PostMapping("update")
    public ResponseEntity<R<YddArticleDto>> updateArticle(@RequestBody YddArticleDto yddArticleDto){
        yddArticleService.updateById(yddArticleDto);
        return ResponseEntity.ok(R.success(null));
    }

}
